//
//  TSPublishViewModel.m
//  TaShuo
//
//  Created by N-X on 2018/3/22.
//  Copyright © 2018年 MRNAN. All rights reserved.
//

#import "TSPublishViewModel.h"
#import "TSPublishCell.h"
#import "TSPublishCellViewModel.h"
#import "TSPublishPathModel.h"
#import "TSPublishHeaderView.h"

@interface TSPublishViewModel()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong)NSMutableArray * pathDatas;
@end
@implementation TSPublishViewModel
- (void)bindView:(UIView *)view
{
    UITableView * tableView = (UITableView *)view;
    tableView.delegate = self;
    tableView.dataSource = self;
    
    //创建headerView
//    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
//    headerView.backgroundColor = [UIColor purpleColor];
//    tableView.tableHeaderView = headerView;
    TSPublishHeaderView * header = [TSPublishHeaderView NX_ViewFromXib];
    header.frame = CGRectMake(0, 0, KScreenWidth, 222);
    tableView.tableHeaderView = header;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 注册cell
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([TSPublishCell class]) bundle:nil] forCellReuseIdentifier:kPublishCellID];
}

- (RACCommand *)requestPulishDataCommand
{
    if (!_requestPulishDataCommand) {
        _requestPulishDataCommand = [[RACCommand alloc]initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            RACSignal * pathSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                [[TSNetTools sharedTSNetTools] GET:@"https://www.yohomars.com/api/v1/line/lines/linelist?app_version=2.2.6&city_id=5176&client_secret=11aef2627fdb63438d81b797465d3d70&client_type=iphone&lang=zh&latitude=31.672036&limit=20&longitude=119.953770&model=iPhone10%2C2&os_version=11.2.6&page=1&screen_size=414x736&session_code=a97cbc53113334edc9b42e56c1e6e08c&udid=221198c14e9656e3b4f4fe080617b20e691cfe34&unique_token=221198c14e9656e3b4f4fe080617b20e691cfe34&v=1" params:nil isShowProgress:NO completion:^(NSError *error, id responseJson, BOOL isCache) {
                    if (!error) {
                        NSInteger code = [responseJson[@"code"] integerValue];
                        if (code == 200) {
                            NSDictionary * dataDict = responseJson[@"data"];
                            NSInteger currentPage = [responseJson[@"currentPage"] integerValue];
                            NSInteger totalPage = [responseJson[@"totalPage"] integerValue];
                            NSArray * list = dataDict[@"list"];
                            NSArray * result = [[[TSPublishPathModel mj_objectArrayWithKeyValuesArray:list].rac_sequence map:^id _Nullable(id  _Nullable value) {
                                TSPublishCellViewModel * cellVM = [[TSPublishCellViewModel alloc]init];
                                cellVM.model = value;
                                return cellVM;
                            }] array];
                            self.pathDatas = [NSMutableArray arrayWithArray:result];
                            [subscriber sendNext:self.pathDatas];
                            [subscriber sendCompleted];
                        }
                    }else
                    {
                        [subscriber sendError:error];
                    }
                }];
                return nil;
            }];
            return pathSignal;
        }];
    }
    return _requestPulishDataCommand;
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.pathDatas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TSPublishCell * cell = [tableView dequeueReusableCellWithIdentifier:kPublishCellID];
    TSPublishCellViewModel * cellVM = self.pathDatas[indexPath.row];
    [cellVM bindView:cell];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 193;
}

@end
